package org.jash.imdemo.fragment;

import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMConversation;

import org.jash.imdemo.ChatActivity;
import org.jash.imdemo.R;

import java.util.Map;


public class SessionFragment extends Fragment {
    private RecyclerView recycler;
    private ConversationAdapter adapter;
    public SessionFragment() {
        super(R.layout.fragment_session);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        recycler = view.findViewById(R.id.recycler);
        adapter = new ConversationAdapter();
        recycler.setAdapter(adapter);
        new Thread(() -> {
            Map<String, EMConversation> conversations = EMClient.getInstance().chatManager().getAllConversations();
            requireActivity().runOnUiThread(() -> adapter.addAll(conversations.values()));
        }).start();
        adapter.setListener(conversation -> startActivity(new Intent(requireContext(), ChatActivity.class).putExtra("toName", conversation.conversationId())));
    }
}